package dao

import javax.inject.Inject
import models.Tables._
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
import slick.jdbc.JdbcProfile

import scala.concurrent.{ExecutionContext, Future}

class GermDao @Inject()(protected val dbConfigProvider: DatabaseConfigProvider)
                       (implicit exec: ExecutionContext) extends
  HasDatabaseConfigProvider[JdbcProfile]  {

  import profile.api._

  def getById(id:Int) : Future[GermRow] = {
    db.run(Germ.filter(_.id === id).result.head)
  }

  def getByIds(ids:Seq[Int]) : Future[Seq[GermRow]] = {
    db.run(Germ.filter(_.id.inSetBind(ids)).result)
  }

  def getAllGerm : Future[Seq[GermRow]] = {
    db.run(Germ.result)
  }

  def getByPos(content:String,types:String) : Future[Seq[GermRow]] ={
    val germ = Germ
    val c = "%" + content + "%"
    val filter = types match {
      case "name" => germ.filter(_.name.like(c))
      case "latin" => germ.filter(_.latin.like(c))
      case "family" => germ.filter(_.family.like(c))
      case "familyLatin" => germ.filter(_.familyLatin.like(c))
      case "genus" => germ.filter(_.genus.like(c))
      case "genusLatin" => germ.filter(_.genusLatin.like(c))
    }
    db.run(filter.result)
  }

}
